package com.yape.stockService.impl;

import com.yape.stockService.IChinaIndexService;
import com.yape.dao.IChinaIndexDao;
import com.yape.po.ChinaIndex;
import com.yape.vo.ChinaIndexVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yape
 * @description
 * @date 2025/5/24 21:08
 */
@Service
public class ChinaIndexService implements IChinaIndexService {

    @Resource
    private IChinaIndexDao chinaIndexDao;

    @Override
    public ChinaIndexVO queryChinaIndexList(int currentPage, int pageSize) {

        // 总记录数
        int count = chinaIndexDao.queryElementsCount();
        // 记录列表
        List<ChinaIndex> elements = chinaIndexDao.queryElements((currentPage - 1) *pageSize, pageSize);

        ChinaIndexVO chinaIndexVO = ChinaIndexVO.builder()
                .totalElements(count)
                .totalPages((count + pageSize - 1) / pageSize)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        // 转换为 VO 内部类 ChinaIndexInner 列表
        List<ChinaIndexVO.ChinaIndexInner> content = elements.stream()
                .map(e -> ChinaIndexVO.ChinaIndexInner.builder()
                        .id(e.getId())
                        .indexName(e.getIndexName())
                        .latestPrice(e.getLatestPrice())
                        .build())
                .toList();

        chinaIndexVO.setContent(content);
        return chinaIndexVO;
    }
}
